{if !empty($perms['parent']) || $userInfo['role'] == 'operator'}

<!-- <div class="form-group"> -->
<!--     <label class="col-lg control-label"></label> -->
<!--     <div class="col-sm-9 col-xs-12"> -->
<!--         <span class='form-control-static'>用户可以在此角色权限的基础上附加其他权限</span> -->
<!--     </div> -->
<!-- </div> -->

{if $action=='userpost'}
<div class="form-group">
    <label class="col-lg control-label">查看权限</label>
    <div class="col-lg-10">
        <label class="radio-inline"><input type="radio" name="look_switch" value="0" {if empty($item['look_switch'])}checked="true"{/if} /> 隐藏</label>
        <label class="radio-inline"><input type="radio" name="look_switch" value="1" {if $item['look_switch'] == 1}checked="true"{/if}/> 查看</label>
    </div>
</div>
<div class="form-group">
    <label class="col-lg control-label">权限设置</label>
    <div class="col-lg-10">
        <label class="radio-inline"><input type="radio" name="is_limit" value="0" {if empty($item['is_limit'])}checked="true"{/if} /> 不限权限</label>
        <label class="radio-inline"><input type="radio" name="is_limit" value="1" {if $item['is_limit'] == 1}checked="true"{/if}/> 限定权限</label>
        <span class="help-block">注意：不限制权限，用户将拥有已分配应用内的所有菜单权限</span>
    </div>
</div>
{/if}

<div class="form-group" id="app-perm" style="{if empty($item['is_limit']) && $action=='userpost'}display:none;{/if}">
    <label class="col-lg control-label">应用权限</label>
    <div class="col-sm-9 col-xs-12">
        {foreach $perms['parent'] as $key => $value}
        <label class="checkbox-inline">
            <input type="checkbox" name="app_perms[]" value="{$key}" {if in_array($key,$role_app_perms) || in_array($key,$app_perms)}checked{/if} {if in_array($key,$role_app_perms) && $action=='userpost'} disabled {/if}>  <b>{$value['text']}</b>
        </label>
        {/foreach}
        <span class="help-block">启用应用权限后，用户将拥有该应用内所有菜单权限</span>
    </div>
</div>

<div class="form-group" id="role-perm" style="{if empty($item['is_limit']) && $action=='userpost'}display:none;{/if}">
    <label class="col-lg control-label">操作权限</label>
    <div class="col-sm-9 col-xs-12">
        <div id="accordion" class="panel-group">
            <div class='panel panel-default'>

                {php}$i=0;{/php}

                {foreach $perms['parent'] as $key => $value}

                    <div class='panel-heading' style="background:#f8f8f8;border-bottom: 1px solid #efefef;display:{if !in_array($key,$operator_perms) && $userInfo['role'] == 'operator'}none{/if};" >
                        <a class="btn btn-link btn-sm pull-right" data-toggle="collapse" data-parent="#accordion" href="#collapse_{$key}"><i class='icon icon-chevron-down'></i> 展开</a>
                        <label class='checkbox-inline'>
                            <input type='checkbox' id="perm_{$key}" value='{$key}' class='perm-all perms' data-group='{$key}'
                                    {if in_array($key,$role_perms) || in_array($key,$user_perms) } checked {/if}
                                    {if in_array($key,$role_perms) && $action=='userpost'} disabled {/if}
                            /> {$value['text']}
                        </label>
                    </div>

                    <div id="collapse_{$key}" class="panel-collapse {if $i==0}in{else}collapse{/if}">

                        <div class='panel-body perm-group'>
                            {if count($perms['parent'][$key]) >1}
                                <span>
                                    {foreach $perms['parent'][$key] as $ke => $val}
                                        {if $ke != 'text'}
                                             <label class='checkbox-inline'>
                                                 <input type='checkbox'  value='{$key}.{$ke}' class='perm-item perms' data-group='{$key}' data-parent='text' checked disabled />  {:str_replace("-log", "", $val)}
                                             </label>
                                        {else}
                                             <label class='checkbox-inline'>
                                                 <input type='checkbox'  value='{$key}' class='perm-all-item perms' data-group='{$key}' data-parent='text' checked disabled />  <b>{:str_replace("-log", "", $val)}</b>
                                             </label>
                                        {/if}
                                    {/foreach}
                                </span>
                                <br>
                            {/if}

                            {if !empty($perms['son'][$key])}

                            {foreach $perms['son'][$key] as $ke => $val}
                                <div class="menu-group">
                                    {if !empty($val)}
                                        <span>
                                        {foreach $val as $k => $v}
                                            {if $k != 'text'}
                                                <label class='checkbox-inline' style="display: {if !in_array($key.".".$ke.".".$k,$operator_perms) && $userInfo['role'] == 'operator'}none;{/if}">
                                                    <input type='checkbox'  value='{$key}.{$ke}.{$k}' class='perm-item perms' data-group='{$key}' data-parent='{$ke}' data-son="{$k}"

                                                        {if in_array($key.".".$ke.".".$k,$role_perms) || in_array($key.".".$ke.".".$k,$user_perms)} checked{/if}
                                                        {if in_array($key.".".$ke.".".$k,$role_perms) && $action=='userpost'} disabled{/if}

                                                    />  {:str_replace("-log", "", $v)}
                                                </label>
                                            {else}
                                                <label class='checkbox-inline' style="display: {if !in_array($key.".".$ke,$operator_perms) && $userInfo['role'] == 'operator'}none;{/if}">
                                                    <input type='checkbox'  value='{$key}.{$ke}' class='perm-all-item perms groups' data-group='{$key}' data-parent='{$ke}' data-son="{$k}"

                                                        {if in_array($key.".".$ke,$role_perms) || in_array($key.".".$ke,$user_perms)} checked{/if}
                                                        {if in_array($key.".".$ke,$role_perms) && $action=='userpost'} disabled{/if}

                                                    />  <b>{:str_replace("-log", "", $v)}</b>
                                                </label>
                                            {/if}
                                        {/foreach}
                                        </span><br>
                                    {/if}

                                    {php}$parentKe = $ke;{/php}
                                    <div style="margin-left: 25px;">
                                        {foreach $perms['grandson'][$key] as $ke => $val}
                                            {foreach $val as $k => $v}
                                                {if !empty($v) && $ke == $parentKe}
                                                    <span>
                                                        {foreach $v as $kk => $vv}
                                                            {if $kk != 'text'}
                                                                 <label class='checkbox-inline'>
                                                                     <input type='checkbox'  value='{$key}.{$ke}.{$k}.{$kk}' class='perm-item perms'
                                                                            data-group='{$key}' data-parent='{$parentKe}' data-son="{$k}" data-grandson="{$kk}"
                                                                            {if in_array($key.".".$ke.".".$k.".".$kk,$role_perms) || in_array($key.".".$ke.".".$k.".".$kk,$user_perms)} checked{/if}
                                                                     {if in_array($key.".".$ke.".".$k.".".$kk,$role_perms)  && $action=='userpost'} disabled{/if}
                                                                     />  {:str_replace("-log", "", $vv)}
                                                                 </label>
                                                            {else}
                                                                 <label class='checkbox-inline'>
                                                                     <input type='checkbox' value='{$key}.{$ke}.{$k}' class='perm-all-item perms groups-item'
                                                                            data-group='{$key}' data-parent='{$parentKe}' data-son="{$k}" data-grandson="{$kk}"
                                                                            {if in_array($key.".".$ke.".".$k,$role_perms) || in_array($key.".".$ke.".".$k,$user_perms)} checked{/if}
                                                                     {if in_array($key.".".$ke.".".$k,$role_perms)  && $action=='userpost'} disabled{/if}
                                                                     />  <b>{:str_replace("-log", "", $vv)}</b>
                                                                 </label>
                                                            {/if}
                                                        {/foreach}
                                                     </span><br>
                                                {/if}
                                            {/foreach}
                                        {/foreach}
                                    </div>
                                </div>
                            {/foreach}
                        {/if}
                        </div>
                    </div>

                    {php}$i++;{/php}
                {/foreach}

            </div>
        </div>
    </div>
    <input type="hidden" name="permsarray" value="">
</div>

<script>
    require(['bootstrap'], function ($) {

        $('.perm-all').click(function () {
            let checked = $(this).get(0).checked;
            let group = $(this).data('group');
            $(".perm-item[data-group='" + group + "'],.perm-all-item[data-group='" + group + "']").each(function () {
                $(this).get(0).checked = checked;
            })
        });

        $('.perm-all-item').click(function () {
            let checked = $(this).get(0).checked;
            let group = $(this).data('group');
            let parent = $(this).data('parent');
            let son = $(this).data('son');
            let grandson = $(this).data('grandson');
            let isGroup = $(this).hasClass('groups');

            $(this).parents("span").find(".perm-item").each(function () {
                $(this).get(0).checked = checked;
            });

            if( isGroup ){
                $(this).parents(".menu-group").find(".groups-item").each(function () {
                    $(this).get(0).checked = checked;
                });
                $(this).parents(".menu-group").find(".perm-item").each(function () {
                    $(this).get(0).checked = checked;
                });
            }else{
                $(this).parents(".menu-group").find(".groups").each(function () {
                    $(this).get(0).checked = checked;
                });
            }

            group_check(this);
        });

        $('.perm-item').click(function () {
            let group = $(this).data('group');
            let parent = $(this).data('parent');
            let son = $(this).data('son');
            let grandson = $(this).data('grandson');
            let check = false;

            $(this).closest('span').find(".perm-item").each(function () {
                if ($(this).get(0).checked) {
                    check = true;
                    return false;
                }
            });

            let allitem = $(this).parents("span").find(".perm-all-item");
            if (allitem.length === 1) {
                allitem.get(0).checked = check;
            }

            group_check(this);
        });

        $(".panel-body").find("span").each(function (index, item) {
            if ($(this).find("label").length !== 1) {
                $($(this).find("label").get(0)).wrap("<div class='col-sm-2' style='white-space:nowrap;'></div>");
                $($(this).find("label").not($(this).find("label").get(0))).wrapAll("<div class='col-sm-10'></div>");
            }
            else {
                $($(this).find("label").get(0)).wrap("<div class='col-sm-12'></div>");
            }
        });

        $("form").submit(function () {
            $(this).attr('stop',1);
            let perms = [];

            $(".perms:checked").each(function (index,e) {
                perms[index]=$(this).val();
            });

            perms = perms.join(',');
            $("input[name=permsarray]").val(perms);
            $(this).attr('stop',0);
            return true;
        });

        $("input[name=look_switch]").click(function(){
            if( $(this).val() == 1 ){
                $("#role-perm").show()
            }else{
                $("#role-perm").hide()
            }
        })

        $("input[name=is_limit]").click(function(){
            if( $(this).val() == 1 ){
                $("#role-perm").show()
            }else{
                $("#role-perm").hide()
            }
        })
    });

    function group_check(obj) {
        let isGroup = $(this).hasClass('groups');
        if( !isGroup ){
            let check2 = false;
            $(obj).parents('.menu-group').find(":checkbox").each(function (index, item) {
                if (item.checked) {
                    check2 = true;
                }
            });
            let group2 = $(obj).eq(0).data('group');
            $(obj).parents(".menu-group").find(".groups[data-group=" + group2 + "]").get(0).checked = check2;
        }

        let check = false;
        $(obj).parents('.perm-group').find(":checkbox").each(function (index, item) {
            if (item.checked) {
                check = true;
            }
        });
        let group = $(obj).eq(0).data('group');
        $(".perm-all[data-group=" + group + "]").get(0).checked = check;
    }
</script>
{/if}